package com.offer;


import java.util.ArrayList;
import java.util.HashSet;

public class Test62 {

    public int lastRemaining(int n, int m) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            arrayList.add(i);
        }

        int idx = 0;

        while (n > 1) {
            idx = (idx + m - 1) % n;
            arrayList.remove(idx);
            n--;
        }

        return arrayList.get(0);
    }

    public static void main(String[] args) {
        Test62 test = new Test62();
//        System.out.println(test.isStraight(new int[]{1, 2, 3, 4, 5}));
        System.out.println(test.lastRemaining(5, 3));
    }

}
